package com.ks.core.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

public class FilterPicVideoUtil {

    private static String picEnds="gif,jpg,jpeg,png,bmp,webp,GIF,JPG,JPEG,PNG,BMP,WEBP";
    private static String videoEnds="swf,avi,flv,mpg,rm,mov,wav,asf,3gp,mkv,rmvb,mp4";
    private static String aliVideoEnds="avi,flv,mp4,mpg,asf,wmv,mov,wma,rmvb,rm,flash,mid,ts";
    private final static Pattern IMG_URL = Pattern.compile("[.](gif|jpeg|png|jpg|bmp|webp)$");
    private final static Pattern VIDEO_URL = Pattern.compile("[.](swf|avi|flv|mpg|rm|mov|wav|asf|3gp|mkv|rmvb|mp4)$");
    private final static Pattern ALI_VIDEO_URL = Pattern.compile("[.](avi|flv||mp4|mpg|asf|wmv|mov|wma|rmvb|rm|flash|mid|ts)$");


    public static Map<String,Object> filterPicVideo(List<String> urlList){
        Map<String,Object> resultMap=new HashMap<String,Object>();
        int containsUnknown=0;//是否包含未知格式的url
        List<String> picList=new ArrayList<String>();
        List<String> videoList=new ArrayList<String>();

        if(urlList!=null && urlList.size()>0){
            for(String url:urlList){
                if(url !=null && !"".equals(url)){
                    String trimUrl=url.trim();
                    int index=trimUrl.lastIndexOf(".");
                    if(index==(-1)) {
                        continue;
                    }else{
                        index++;
                        String ext=trimUrl.substring(index).toLowerCase();
                        if(ext!=null&& !"".equals(ext)){  //空字符串肯定被包含
                            if(picEnds.contains(ext)){
                                picList.add(url);
                                continue;
                            }else if(aliVideoEnds.contains(ext)){
                                videoList.add(url);
                                continue;
                            }else{
                                containsUnknown=1;
                                break;
                            }
                        }

                    }
                }
            }
        }

        resultMap.put("containsUnknown",containsUnknown);
        resultMap.put("picList",picList);
        resultMap.put("videoList",videoList);
        return resultMap;
    }

    //待测试
    public static Map<String,Object> patternPicVideo(List<String> urlList){
        Map<String,Object> resultMap=new HashMap<String,Object>();
        int containsUnknown=0;//是否包含未知格式的url
        List<String> picList=new ArrayList<String>();
        List<String> videoList=new ArrayList<String>();

        if(urlList!=null && urlList.size()>0){
            for(String url:urlList){
                if(url !=null && !"".equals(url)){
                    String trimUrl=url.trim();
                    if(IMG_URL.matcher(trimUrl).matches()){
                        picList.add(url);
                        continue;
                    }else if(ALI_VIDEO_URL.matcher(trimUrl).matches()){
                        videoList.add(url);
                        continue;
                    }else{
                        containsUnknown=1;
                        break;
                    }
                }
            }
        }

        resultMap.put("containsUnknown",containsUnknown);
        resultMap.put("picList",picList);
        resultMap.put("videoList",videoList);
        return resultMap;
    }


}
